//
//  ChoicenessViewController.m
//  LubanWork
//
//  Created by xiaohao on 2019/12/31.
//  Copyright © 2019 xiaohao. All rights reserved.
//

#import "ChoicenessViewController.h"
#import "choicenessTableViewCell.h"
#import "ChoicenessModel.h"
#import "GotoLoginView.h"
#import "WorkDetailViewController.h"

@interface ChoicenessViewController ()<UITableViewDelegate,UITableViewDataSource,choicenessDelegate>
{
    NSString *jzid;
    MJRefreshAutoNormalFooter *footer;
}
@property(nonatomic,strong) ChoicenessModel * model;
@property(nonatomic,strong) UITableView * table;
@property(nonatomic,strong) NSMutableArray * titArray;
@property(nonatomic,assign) NSInteger page;


@end

@implementation ChoicenessViewController
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}
- (void)viewDidLoad {
    [super viewDidLoad];


   
    self.page = 1;
    self.titArray = [[NSMutableArray alloc] init];
    self.navigationItem.title = @"精选兼职";
    
    [self setTable];
    
    [self loadData];
}
-( void)setTable{
    [self setExtraCellLineHidden:self.table];
    [self.table setSeparatorInset:UIEdgeInsetsZero];
    [self.view addSubview:self.table];
    
}
- (void)setExtraCellLineHidden:(UITableView *)tab{
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor clearColor];
    [tab setTableFooterView:view];
}
-(UITableView *)table{
    if (!_table) {
        _table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-kTabHeight-kStatusBarHeight) style:UITableViewStylePlain];
        _table.delegate = self;
        _table.dataSource = self;
        _table.separatorStyle = UITableViewCellSeparatorStyleNone;
        _table.mj_header=[MJRefreshNormalHeader headerWithRefreshingBlock:^{
            self.page = 1;
            [self loadData];
        }];
         
        footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(getMoreVideo)];
        [footer setTitle:@"正在加载更多..." forState:MJRefreshStateRefreshing];
        [footer setTitle:@"暂无更多" forState:MJRefreshStateNoMoreData];
        footer.stateLabel.font = [UIFont systemFontOfSize:15.0f];
        footer.automaticallyHidden = YES;
        _table.mj_footer = footer;
    }
    return _table;
}
-(void)getMoreVideo{
    self.page ++;
    [self loadData];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.titArray.count;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return KSCHEIGHT(130);
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] init];
    
    view.backgroundColor = kColorFromRGB(249, 247, 255);
    return view;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *identifier = [NSString stringWithFormat:@"%ld,%ld",[indexPath section],[indexPath row]];
    choicenessTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (!cell) {
        cell = [[choicenessTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
    }
    
    ChoicenessModelResult *list = self.titArray[indexPath.row];
    cell.titLab.text = list.name;
    cell.priceLab.text = [list.wagemax stringValue];
    cell.willSignLab.text = [NSString stringWithFormat:@"已报名:%@",list.signup];
    //    日结丨长期丨男女不限
    
    
    NSString *str = [list.wagetype isEqualToNumber:@1] == YES ? @"日结":@"月结";
    
    
    NSString *str1 = [list.jobtime isEqualToNumber:@1] == YES ? @"长期":@"短期";
    NSString *str2;
    if ([list.sex isEqualToNumber:@1]) {
        str2 = @"男";
    }else if ([list.sex isEqualToNumber:@2]){
        str2 = @"女";
    }else{
        str2 = @"男女不限";
    }
    
    cell.totalLab.text = [NSString stringWithFormat:@"%@丨%@丨%@",str,str1,str2];
    int k = arc4random()%19;
    NSString *ks = [NSString stringWithFormat:@"%d",k];
    NSLog(@"%@",ks);
    cell.backgourd.image = [UIImage imageNamed:ks];
    cell.zjID = list.Id;
    jzid = list.Id;
    cell.delegate = self;
    cell.model = list;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}
-(void)loadData{
    
    [footer setTitle:@"点击或上拉加载更多" forState:MJRefreshStateIdle];
    NSDictionary *dic  = @{
        @"pageNo":[NSString stringWithFormat:@"%ld",(long)self.page],
        @"pageSize":@"10",
        @"choice":@"1",
    };
    __weak typeof(self) weakSelf = self;
    [[NetWorkManager sharedManager] getData:@"api/jianzhi/getJianzhi" parameters:dic success:^(NSURLSessionDataTask *task, id responseObject) {
        
        if ([responseObject[@"code"] isEqualToNumber:@200]) {
            
            NSArray *arr = responseObject[@"result"];
            if (weakSelf.page == 1) {
                [weakSelf.titArray removeAllObjects];
            }
            weakSelf.model = [ChoicenessModel yy_modelWithJSON:responseObject];
            if (arr.count <= 0) {
                //                [weakSelf.table.mj_footer endRefreshingWithNoMoreData];
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self->footer setTitle:@"暂无更多数据" forState:MJRefreshStateIdle];
//                    if ([self.table.mj_footer isRefreshing]) {
//                        self.table.mj_footer.hidden = YES;
//                    }
                });
            }else{
                [weakSelf.titArray addObjectsFromArray:weakSelf.model.result];
            }
            //
            if (self.model.result.count > 0) {
                [weakSelf.table.mj_footer endRefreshingWithNoMoreData];
            }else{
                
            }
            if (arr == 0) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self->footer setTitle:@"暂无更多" forState:MJRefreshStateIdle];
                });
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                [weakSelf.table reloadData];
                [weakSelf.table.mj_footer endRefreshing];
                [weakSelf.table.mj_header endRefreshing];
            });
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakSelf.table reloadData];
            [weakSelf.table.mj_footer endRefreshing];
            [weakSelf.table.mj_header endRefreshing];
        });
    }];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ChoicenessModelResult *list = self.titArray[indexPath.row];
    
    WorkDetailViewController *work = [[WorkDetailViewController alloc] init];
    work.detailID = list.Id;
    [self.navigationController pushViewController:work animated:YES];

}
 
-(void)clickWithDiselectWithCell:(choicenessTableViewCell *)cell{
    
    ChoicenessModelResult *list = cell.model;
    GotoLoginView *gologin = [[GotoLoginView alloc] init];
    [[NetWorkManager sharedManager] getData:@"api/jianzhi/addSignup" parameters:@{
        @"jzId":list.Id
    } success:^(NSURLSessionDataTask *task, id responseObject) {
        if ([responseObject[@"code"] isEqualToNumber:@200]) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [gologin show];
                
            });
        }else{
            dispatch_async(dispatch_get_main_queue(), ^{
                //                    [gologin dismiss];
                [HWProgressHUD showMessage:@"已报名"];
                //                _bottomView.SignBtn.selected = YES;
            });
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        [gologin dismiss];
    }];
    
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat sectionHeaderHeight = 44;//这里的高度是设置的sectionView的高度
    
    if(scrollView.contentOffset.y <= sectionHeaderHeight && scrollView.contentOffset.y >= 0) {
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
        
    } else if (scrollView.contentOffset.y >= sectionHeaderHeight)
    {
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
    }
}
@end

